package com.atguigu.gmallsuger.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallsuger.bean.ProductStats;
import com.atguigu.gmallsuger.bean.ProvinceStats;
import com.atguigu.gmallsuger.bean.VisitorStats;
import com.atguigu.gmallsuger.service.KeywordService;
import com.atguigu.gmallsuger.service.ProductStatsService;
import com.atguigu.gmallsuger.service.ProvinceStatsService;
import com.atguigu.gmallsuger.service.VisitorStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/4/28 14:12
 */
@RestController
@RequestMapping("/api/suger")
public class SugarController {
    
    @Autowired
    ProductStatsService productStats;
    
    @RequestMapping("/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        BigDecimal gmv = productStats.getGMV(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        return result.toJSONString();
    }
    
    @RequestMapping("/trademark")
    public String getProductStatsByTM(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<ProductStats> pss = productStats.getProductStatsByTM(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (ProductStats stats : pss) {
            categories.add(stats.getTm_name());
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        JSONObject ser = new JSONObject();
        ser.put("name", "商品品牌");
        JSONArray data1 = new JSONArray();
        for (ProductStats stats : pss) {
            data1.add(stats.getOrder_amount());
        }
        ser.put("data", data1);
        
        series.add(ser);
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/spu")
    public String getProductStatsBySpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<ProductStats> pss = productStats.getProductStatsBySpu(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (ProductStats stats : pss) {
            JSONObject obj = new JSONObject();
            obj.put("name", stats.getSpu_name());
            obj.put("value", stats.getOrder_amount());
            
            data.add(obj);
        }
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/c3")
    public String getProductStatsByC3(@RequestParam(value = "date", defaultValue = "0") int date,
                                      @RequestParam(value = "limit", defaultValue = "5") int limit) {
        System.out.println(limit);
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<ProductStats> pss = productStats.getProductStatsByC3(date, limit);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "三级品牌名字");
        c1.put("id", "category3_name");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        JSONObject c3 = new JSONObject();
        c3.put("name", "订单量");
        c3.put("id", "order_ct");
        columns.add(c3);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (ProductStats stats : pss) {
            JSONObject row = new JSONObject();
            row.put("category3_name", stats.getCategory3_name());
            row.put("order_amount", stats.getOrder_amount());
            row.put("order_ct", stats.getOrder_ct());
            
            rows.add(row);
            
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    ProvinceStatsService provinceStats;
    
    @RequestMapping("/province")
    public String province(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<ProvinceStats> pss = provinceStats.getProvinceStatsByName(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (ProvinceStats ps : pss) {
            JSONObject p = new JSONObject();
            p.put("name", ps.getProvince_name());
            p.put("value", ps.getOrder_amount());
            
            p.put("tooltipValues", new Object[]{ps.getOrder_count()});
            
            mapData.add(p);
        }
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        data.put("tooltipNames", new Object[]{"订单数"});
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    VisitorStatsService visitorStats;
    
    @RequestMapping("/visitor_new")
    public String visitorHr(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<VisitorStats> vss = visitorStats.getVisitorStatsByIsNew(date);
        
       
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        data.put("total", vss.size());
    
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "新(1)旧(0)用户");
        c1.put("id", "is_new");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "UV");
        c2.put("id", "uv");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "PV");
        c3.put("id", "pv");
        columns.add(c3);
        
        JSONObject c4 = new JSONObject();
        c4.put("name", "进入次数");
        c4.put("id", "sv");
        columns.add(c4);
        
        JSONObject c5 = new JSONObject();
        c5.put("name", "跳出次数");
        c5.put("id", "uj");
        columns.add(c5);
        
        
        JSONObject c6 = new JSONObject();
        c6.put("name", "跳出率");
        c6.put("id", "uj_rate");
        columns.add(c6);
        
        data.put("columns", columns);
    
        JSONArray rows = new JSONArray();
        
        data.put("rows", rows);
        for (VisitorStats vs : vss) {
            JSONObject row = new JSONObject();
            row.put("is_new", vs.getIs_new());
            row.put("uv", vs.getUv_ct());
            row.put("pv", vs.getPv_ct());
            row.put("sv", vs.getSv_ct());
            row.put("uj", vs.getUj_ct());
            row.put("uj_rate", new DecimalFormat("0.00%").format(vs.getUj_ct() / vs.getSv_ct()));
            rows.add(row);
        }
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    
    @Autowired
    KeywordService ks;
    @RequestMapping("/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date,
                     @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            // 如果没有传递日期, 使用今天
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
        List<JSONObject> objs = ks.getKewWordStats(date, limit);
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
        for (JSONObject kwStats : objs) {
            JSONObject dataObj = new JSONObject();
            dataObj.put("name", kwStats.getString("keyword"));
            dataObj.put("value", kwStats.getLong("score"));
            data.add(dataObj);
            
        }
    
        result.put("data", data);
        
        return result.toJSONString();
    }
}
